<template>
  <div class="upload-item">
    <el-upload ref="uploadfileRef" :accept="type" class="avatar-uploader" action="/api/common/upload"
      :show-file-list="false" :on-success="handleAvatarSuccess" :on-error="handleError"
      :before-upload="beforeAvatarUpload" :headers="headers" drag>
      <div class="image">
        <img v-if="imageUrl" :src="imageUrl" class="avatar" />
        <el-icon v-else class="avatar-uploader-icon">
          <Plus />
        </el-icon>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      </div>
      <template #tip>
        <div class="el-upload__tip" v-show="!imageUrl"> jpg/jpeg/png files with a size less than 500kb </div>
      </template>
    </el-upload>
  </div>
</template>

<script lang="ts" setup>
import { ref, watch } from 'vue'
import { Plus } from '@element-plus/icons-vue'
import { ElMessage, UploadProps } from 'element-plus'
const props = defineProps<{ image: string }>()
// 上传图片类型
const type = ref('image/*')
const emit = defineEmits(['onImageChange'])
const imageUrl = ref(props.image)
watch(
  () => props.image,
  newVal => {
    imageUrl.value = newVal
  },
)
const headers = {
  token: localStorage.getItem('token') || '',
}

const handleAvatarSuccess: UploadProps['onSuccess'] = (response, _uploadFile) => {
  console.log(response.data)
  imageUrl.value = `${response.data}`
  emit('onImageChange', imageUrl.value)
}
// 上传失败
const handleError: UploadProps['onError'] = (error, file, fileList) => {
  console.log(error, file, fileList)
  ElMessage.error('图片上传失败')
}
// 添加beforeAvatarUpload钩子来验证文件类型和大小
const beforeAvatarUpload = (file: { type: string; size: number }) => {
  const isImage = file.type.startsWith('image/')
  const isLt500k = file.size / 1024 < 500 // 限制文件大小为500KB
  if (!isImage) {
    ElMessage.error('请上传图片文件')
    return false
  }
  if (!isLt500k) {
    ElMessage.error('图片大小不能超过500KB')
    return false
  }
  return true
}
</script>
<style lang="scss" scoped>
.upload-item {
  width: 300px;
  height: 300px;
  display: flex;
  align-items: center;

  .avatar-uploader {
    height: 100%;
    width: 100%;
    display: inline-block;
    overflow: hidden;
  }

  .avatar-uploader .image {
    width: 278px;
    height: 197px;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;

    // overflow: hidden;
    .avatar {
      height: 100%;
      width: 100%;
      object-fit: cover;
    }
  }

  .el-upload-list__item-actions {
    position: absolute;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    cursor: default;
    text-align: center;
    color: #fff;
    opacity: 0;
    font-size: 20px;
    background-color: rgba(0, 0, 0, 0.5);
    transition: opacity 0.3s;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
  }

  .el-upload-list__item-actions:hover {
    opacity: 1;
  }
}

// 图片上传
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  width: 180px;
  height: 160px;
  font-size: 28px;
  color: #8c939d;
  text-align: center;
}

</style>
